import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { TLabel } from 'src/entities/label.entity';
import { Response } from 'src/utils/response';
import { InjectRepository } from '@nestjs/typeorm';

@Injectable()
export class LabelService {
  constructor(
    @InjectRepository(TLabel)
    private readonly labelRepository: Repository<TLabel>,
  ) {}

  // 增加
  async insertLabelService(labelEntity: TLabel) {
    labelEntity.delFlag = 0;
    await this.labelRepository.save(labelEntity);
    return Response.success({ message: '添加成功' });
  }
  // 删除
  async removeLabelService(id: number) {
    await this.labelRepository.update(id, { delFlag: 1 });
    return Response.success({ message: '删除成功' });
  }
  // 更新
  async updateLabelService(labelEntity: TLabel) {
    await this.labelRepository.update(labelEntity.id, labelEntity);
    return Response.success({ message: '更新成功' });
  }
  // 查询标签数据
  async queryLabelService() {
    const labelList = await this.labelRepository.find({
      where: { delFlag: 0 },
    });
    return Response.success({ data: labelList });
  }

  // 获取标签列表
  async getLabelListService() {
    const labelList = await this.labelRepository.find({
      where: { delFlag: 0 },
    });
    const labelVOList = labelList.map((label) => ({
      id: label.id,
      title: label.title,
    }));
    return Response.success({ data: labelVOList });
  }
}
